package com.lc.demo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lc.demo.bean.*;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.*;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.*;

/**
 * @ClassName TeacherController
 * @Deacription TODO
 * @Author jinshuhan
 * @Version 1.0
 **/
@CrossOrigin
@RestController
@RequestMapping("/teacher")
public class TeacherController {


    @Autowired
    private TeacherService teacherService;
/*
    @Autowired
    private StudentService studentService;*/

    @Autowired
    private ScoreService scoreService;

    @Autowired
    SubjectService subjectService;



    @PostMapping(value = "/login")
    public ResponseMessage login(@RequestBody Teacher teacher) {
        Teacher teacher1 = teacherService.login(teacher.getUsername(), teacher.getPassword());
        if (teacher1 != null) {
            return ResponseMessage.successWithMessage(teacher1.getUsername());
        } else {
            return ResponseMessage.errorWithMessage("用户名或密码错误");
        }
    }

    //根据教师id 查询出 所有的课程  三级列表
    @PostMapping(value = "/findSubjectListByTeacherId")
    public ResponseMessage findSubjectListByTeacherId(@RequestBody Teacher teacher) {

        Teacher teacherSelect = subjectService.findTeacherByUsername(teacher.getUsername());
        List<Map<String,Object>> returnList= subjectService.findSubjectListByTeacherId(teacherSelect.getId());
        return ResponseMessage.successWithData(returnList);
    }
    //根据 课程Id 查询班级列表
    @GetMapping(value = "/findClassListBySubjectId")
    public ResponseMessage findClassListBySubjectId(Integer subjectId) {
        List<Classgrade> classgradeList = subjectService.findClassListBySubjectId(subjectId);

        return ResponseMessage.successWithData(classgradeList);
    }

    //根据 班级id 查询学生列表
    @GetMapping(value = "/findStudentListByClassId")
    public ResponseMessage findStudentListByClassId(Integer classId) {
        List<Student> studentList = subjectService.findStudentListByClassId(classId);
        return ResponseMessage.successWithData(studentList);
    }

    //添加成绩
    @PostMapping(value = "/addScore")
    public ResponseMessage addScore(@RequestBody Score score) {
        scoreService.addScore(score);
        return ResponseMessage.success();
    }
    @PostMapping(value = "/teacherReview")
    public ResponseMessage teacherReview(@RequestBody Score score) {
        scoreService.teacherReview(score);
        return ResponseMessage.success();
    }


    //查询成绩列表
    @PostMapping(value = "/getScoreList")
    public ResponseMessage getScoreList(@RequestBody Teacher teacher) {
        // String username = (String) session.getAttribute("loginUser");
        int pageNum = 1;
        int pageSize = 10;
        if (teacher.getPageNum() != null) {
            pageNum = teacher.getPageNum();
        }
        if (teacher.getPageSize() != null) {
            pageSize = teacher.getPageSize();
        }
        Teacher teacherByUsername = subjectService.findTeacherByUsername(teacher.getUsername());
        PageHelper.startPage(pageNum, pageSize);
        List<Score> scoreList = scoreService.findScoreListTeacher(teacherByUsername.getId());
        PageInfo<Score> page = new PageInfo<>(scoreList);

        return ResponseMessage.successWithData(page);
    }



}
